package offer;

import java.util.Arrays;

public class offerII_0075 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int len1=arr1.length;
        int len2=arr2.length;
        Arrays.sort(arr1);
        int[] result=new int[len1];
        int index=0;
        for(int i=0;i<len2;++i){
            for(int j=0;j<len1;++j){
                if(arr1[j]==arr2[i]){
                    result[index]=arr2[i];
                    index++;
                    arr1[j]=-1;
                }
                if(arr1[j]>arr2[i]) break;
            }
        }
        for(int i=0;i<len1;++i){
            if(arr1[i]!=-1){
                result[index]=arr1[i];
                index++;
            }
        }
        return result;
    }
}
